<template>
  <div>
    <el-button @click="fileDown" type="primary" :loading="loading">点击下载</el-button>
    <el-radio-group v-model="usageId">
      <el-radio :label="key" v-for="(val,key,index) in dict_usage" :key="index">{{val}}</el-radio>
    </el-radio-group>
  </div>
</template>

<script>
  import {
    maintain_meta_data
  } from '@/api/meta.js'
  export default {
    data() {
      return {
        str: '',
        dict_usage: {
          1: '科学选材',
          2: '大纲考核',
          3: '机能监控',
          4: '疾病筛查'
        },
        usageId: "2",
        loading: false,
      }
    },
    created() {
    },
    methods: {
      fileDown() {
        this.loading = true
        this.getMetaData()
      },
      getMetaData() {
        maintain_meta_data({usageId:this.usageId}).then(res => {
          let obj = {}
          let metaName2packageName = {}
          res.data.forEach(item => {
            if (!obj[item.date]) {
              obj[item.date] = {}
            }
            if (!obj[item.date][item.coachName]) {
              obj[item.date][item.coachName] = []
            }
            if (!obj[item.date][item.coachName][item.playerName]) {
              obj[item.date][item.coachName][item.playerName] = []
            }
            obj[item.date][item.coachName][item.playerName].push(item)
            metaName2packageName[item.metaName] = [item.packageName]
          })
          let str = ''
          for (let date in obj) {
            str += '\n' + date + '\n'
            for (let coach in obj[date]) {
              // 该教练的所有指标
              let header = new Set()
              let val = {}
              let currentCoachData = obj[date][coach]

              for (let player in currentCoachData) {

                currentCoachData[player].forEach(metaInfo => header.add(metaInfo.metaName))

              }



              let tableHeader = Array.from(header)
              let pureHeader = Array.from(header)
              tableHeader.unshift("姓名")
              tableHeader.unshift("教练")
              let valStr = ''
              // 这里是每一个运动员
              // player对象
              for (let player in currentCoachData) {
                valStr += coach + ','
                valStr += player + ','
                pureHeader.forEach(head => {
                  let found = false;
                  currentCoachData[player].forEach(metaInfo => {
                    if (metaInfo['metaName'] == head) {
                      found = true;
                      valStr += metaInfo.valueDisplay + ","
                    }
                  })
                  if (!found) {
                    valStr += ","
                  }
                })
                valStr += "\n"
              }
              let packageStr = ""
              tableHeader.forEach(header => {
                if (metaName2packageName[header]) {
                  packageStr += metaName2packageName[header] + ","

                } else {
                  packageStr += "基本信息,"
                }
              })
              str += packageStr + "\n" + tableHeader + '\n' + valStr + '\n'
            }
          }
          this.str = str
        }).then(()=>{
          var data = this.str
          var fileName = this.dict_usage[this.usageId]+"数据.csv";
          data = "\ufeff" + data;
          var blob = new Blob([data], {
            type: 'text/csv,charset=UTF-8'
          });
          var csvUrl = URL.createObjectURL(blob);
          let link = document.createElement('a')
          link.href = csvUrl
          link.download = fileName
          link.click();
          this.loading = false
        })
      },
    }
  }
</script>

<style scoped>

</style>
